package models.user;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import models.logs.LoginLogs;
import models.logs.LoginToken;
import play.Logger;
import play.db.ebean.Model;
import utils.MengException;
import utils.StringUtils;

/**
 * 用户注册表
 * @author zhangpeng
 *
 */

@Entity
@Table(name="user_info")
public class UserInfo extends Model {
	
	private static final long serialVersionUID = 1L;
	
	@Id
	public long id;
	public String username;
	public String password;
	@Transient
	public String repassword;
	@Transient
	public String YinXiangMa_challenge;
	@Transient
	public String YinXiangMa_response;
	@Transient
	public String YXM_level;
	@Transient
	public String YinXiangMa_pk;
	@Transient
	public String YXM_input_result;
	public String email;
	public String mobile;
	/**
	 * 注册时间
	 */
	public String register_time;
	public boolean status = true;
	
	
	public static Model.Finder<Long, UserInfo> find = new Model.Finder<Long, UserInfo>(Long.class, UserInfo.class);
	
	public static UserInfo getInstances(){
		return new UserInfo();
	}
	
	/**
	 * 用户注册
	 * @param ui
	 * @return username
	 */
	public String register(UserInfo ui) throws MengException{
		
		ui.password = StringUtils.md5(ui.password);
		ui.register_time = String.valueOf(StringUtils.getTimeStamp());
		if(!verifyUsername(ui.username)){
			throw new MengException("100001","用户名已存在");
		}
		if(!verifyEmail(ui.email)){
			throw new MengException("100002","邮箱已存在");
		}
		if(!verifyMobile(ui.mobile)){
			throw new MengException("1000021","手机号码已存在");
		}
		try{
			ui.save();
			registerOtherInfo(ui);
		}catch(Throwable t){
			t.printStackTrace();
			Logger.error("UserInfo Register Exception," + t.getMessage());
			throw new MengException("100003","用户注册失败");
		}
		return ui.username;
	}
	
	private void registerOtherInfo(UserInfo ui) throws MengException {
		BaseUserInfo.getInstances().createBUI(ui);
		LinkUserInfo.getInstances().createLUI(ui);
		EduUserInfo.getInstances().createEUI(ui);
		PersonUserInfo.getInstances().createPSUI(ui);
		WorkUserInfo.getInstances().createWUI(ui);
		PortraitUserInfo.getInstances().createPUI(ui);
	}
	
	/**
	 * 用户登陆
	 * @param username
	 * @param password
	 * @return username
	 */
	public String login(String username, String passwd) throws MengException{
		
		if(verifyUsername(username)){
			throw new MengException("100004","用户名不存在");
		}
		
		if(!getPassword(username).equals(StringUtils.md5(passwd))){
			throw new MengException("100005","密码不正确");
		}
		if(!verifyStatusUsername(username)){
			throw new MengException("100016","用户没有激活或者被禁用");
		}
		
		//更新用户登陆日志
		LoginLogs ll = LoginLogs.getInstances();
		ll.username = username;
		LoginLogs.getInstances().createLoginLogs(ll);
		
		//产生登陆令牌
		LoginToken.getInstances().createLoginToken(username);
		
		return username;
	}
	
	/**
	 * 忘记密码
	 * @param email
	 */
	public boolean forgetPwd(String email) throws MengException{
		if(verifyEmail(email)){
			throw new MengException("100006","邮箱不存在");
		}
		try{
			//TODO 发送验证码邮件
			Logger.info("email send...");
			return true;
		}catch(Throwable t){
			t.printStackTrace();
			Logger.error("UserInfo forgetPwd Exception," + t.getMessage());
			throw new MengException("100007","发送邮件失败");
		}
	}
	
	/**
	 * 修改密码
	 * @param username
	 * @param new_pwd
	 * @param old_pwd
	 * @param flag true:验证旧密码 
	 *  
	 * 忘记密码不验证旧密码
	 */
	public boolean modifyPwd(String username, String new_pwd, String old_pwd, boolean flag) throws MengException{
		if(verifyUsername(username)){
			throw new MengException("100008","用户名不存在");
		}
		
		if(!verifyStatusUsername(username)){
			throw new MengException("100017","用户没有激活或者被禁用");
		}
		
		if(flag && !getPassword(username).equals(StringUtils.md5(old_pwd))){
			throw new MengException("100009","密码不正确");
		}
		UserInfo ui = getUserInfoUsername(username);
		ui.password = StringUtils.md5(new_pwd);
		try{
			ui.update();
			return true;
		}catch(Throwable t){
			t.printStackTrace();
			Logger.error("UserInfo modifyPwd Exception," + t.getMessage());
			throw new MengException("100010","修改密码失败");
		}
	}
	
	/**
	 * 修改用户信息
	 * @param ui
	 * @return
	 */
	public boolean modifyUserInfo(UserInfo ui) throws MengException{
		if(verifyUsername(ui.username)){
			throw new MengException("100011","用户名不存在");
		}
		if(!verifyEmail(ui.email)){
			if(!getUserInfoEmail(ui.email).username.equals(ui.username)){
				throw new MengException("100012","邮箱已存在");
			}
		}
		if(!verifyStatusUsername(ui.username)){
			throw new MengException("100018","用户没有激活或者被禁用");
		}
		//TODO 同步联系方式email
		UserInfo user = getUserInfoUsername(ui.username);
		user.email = ui.email;
		try{
			user.update();
			return true;
		}catch(Throwable t){
			t.printStackTrace();
			Logger.error("UserInfo modifyUserInfo Exception," + t.getMessage());
			throw new MengException("100013","修改用户信息失败");
		}
	}
	
	
	/**
	 * 获取用户信息
	 * @param email
	 * @return
	 */
	public UserInfo getUserInfoEmail(String email) throws MengException{
		try{
			return find.where().eq("email", email).findUnique();
		}catch(Throwable t){
			t.printStackTrace();
			Logger.error("UserInfo getUserInfoEmail Exception," + t.getMessage());
			throw new MengException("100014","获取用户信息失败");
		}
	}
	
	/**
	 * 获取用户信息
	 * @param page
	 * @param size
	 * @return
	 */
	public List<UserInfo> getUserInfo(int page,int size) throws MengException{
		try{
			return find.findPagingList(size).getPage(page).getList();
		}catch(Throwable t){
			t.printStackTrace();
			Logger.error("UserInfo getUserInfo Exception," + t.getMessage());
			throw new MengException("100021","获取用户信息列表失败");
		}
	}
	
	/**
	 * 获取用户信息
	 * @param username
	 * @return
	 */
	public UserInfo getUserInfoUsername(String username) throws MengException{
		try{
			return find.where().eq("username", username).findUnique();
		}catch(Throwable t){
			t.printStackTrace();
			Logger.error("UserInfo getUserInfoEmail Exception," + t.getMessage());
			throw new MengException("100015","获取用户信息失败");
		}
	}
	
	/**
	 * 禁用用户信息
	 * @param username
	 * @return
	 */
	public boolean disableUserInfo(String username) throws MengException{
		try{
			UserInfo ui = getUserInfoUsername(username);
			ui.status = false;
			ui.update();
			return true;
		}catch(Throwable t){
			t.printStackTrace();
			Logger.error("UserInfo disableUserInfo Exception," + t.getMessage());
			throw new MengException("100019","禁用用户信息失败");
		}
	}

	/**
	 * 激活用户信息
	 * @param username
	 * @return
	 */
	public boolean activeUserInfo(String username) throws MengException{
		try{
			UserInfo ui = getUserInfoUsername(username);
			ui.status = true;
			ui.update();
			return true;
		}catch(Throwable t){
			t.printStackTrace();
			Logger.error("UserInfo activeUserInfo Exception," + t.getMessage());
			throw new MengException("100020","激活用户信息失败");
		}
	}
	
	/**
	 * 获取密码
	 * @param username
	 * @return password
	 */
	private String getPassword(String username) throws MengException{
		try{
			return find.where().eq("username", username).findUnique().password;
		}catch(Throwable t){
			t.printStackTrace();
			Logger.error("UserInfo getPassword Exception," + t.getMessage());
			throw new MengException("100015","获取密码失败");
		}
	}
	
	/**
	 * 验证用户名唯一性
	 * @param username
	 * @return
	 */
	public boolean verifyUsername(String username) throws MengException{
		if(getUserInfoUsername(username) == null){
			return true;
		}
		return false;
	}
	
	/**
	 * 验证电子邮箱唯一性
	 * @param email
	 * @return
	 */
	public boolean verifyEmail(String email) throws MengException{
		if(getUserInfoEmail(email) == null){
			return true;
		}
		return false;
	}
	
	public boolean verifyMobile(String mobile) throws MengException{
		UserInfo ui = find.where().eq("mobile", mobile).findUnique();
		if( ui == null){
			return true;
		}
		return false;
	}
	
	/**
	 * 验证用户名有效性
	 * @param username
	 * @return
	 */
	public boolean verifyStatusUsername(String username) throws MengException{
		if(getUserInfoUsername(username).status){
			return true;
		}
		return false;
	}
	
	

}
